package com.tgy.algorithm.base;

import java.util.HashMap;
import java.util.Map;

public class _概率转换 {

    public static void test01(){

        int end = 1000000;
        int count = 0;
        for (int j = 0; j < end; j++) {
            boolean val = Math.random() <= 0.5;
            if(val) {
                count++;
            }
        }

        System.out.println((double) count / (double) end);
    }

    public static int random1T5() {
        return  (int)(Math.random() * 5) + 1;
    }

    public static void test02(){

        Map<Integer,Integer> numCounts = new HashMap();
        for (int i = 0; i < 10000000; i++) {
            int i1 = random1T7();
            numCounts.put(i1,numCounts.getOrDefault(i1,0) + 1);
        }
        numCounts.entrySet().forEach(entry-> System.out.println(entry.getKey() + ":" + entry.getValue()));
    }

    public static int randomZO() {

        while (true) {
            int result = random1T5();
            if(result <3) {
                return 0;
            }else if(result >3) {
                return 1;
            }
        }
    }

    public static void test03(){
        int num = 1000000;
        int count = 0;
        for (int i = 0; i < num; i++) {
            if(randomZO() == 1) {
                count++;
            }
        }

        System.out.println((double) count / (double) num);
    }

    // 1- 7
    public static int random1T7() {
//        return  (int)(Math.random() * 7) + 1;
        // 7 111
        while (true) {
            // 随机生产0到7到数字，当生产当是0的时候，重新生成，直到不是7为止
            int result =   (randomZO() << 2) + (randomZO() << 1) + randomZO();
            if (result != 0) {
                return result;
            }
        }
    }

    public static void main(String[] args) {
        test02();
    }
}
